package com.tledu.jdbc;

import java.sql.*;

/**
 * 查询操作
 */
public class Jdbc3 {
    public static void main(String[] args) throws ClassNotFoundException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(
                    "jdbc:mysql://127.0.0.1:3306/jingmao19?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8",
                    "root",
                    "root"
            );
            String sql = "select * from stu where name like ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, "张%");
            // 和插入操作，最主要的变化，在执行sql的语句上
            // executeQuery 执行查询操作，得到结果
            ResultSet resultSet = preparedStatement.executeQuery();
            // 可以通过类似于迭代器的方式进行遍历
            // next() 如果有下一个值，会返回true，没有会返回false
            while (resultSet.next()) {
                // 执行了next()方法之后代表获取下一个值
                System.out.println(resultSet.getString("name"));
                System.out.println(resultSet.getInt("id"));
                System.out.println(resultSet.getInt("age"));
                System.out.println("========================================");
            }
            preparedStatement.close();
            connection.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }
}
